/**
 * 岗位数据表格
 */
Ext.namespace("Ext.ux.grid");

Ext.ux.grid.PosGrid = Ext.extend(Ext.ux.grid.CrudGrid, {

	//是否启用行双击事件的事件处理函数，取值为true，为grid添加默认的行双击事件处理函数，取值为false不添加
	enabledDefaultRowDblclickEvent: false,
	
	posType: null,
	initComponent: function(){
		var gridStructure = [{ 
			name: 'posId',
			hidden: true
		},{
			name: 'posCode',  
			header: "岗位编号",
			sortable: true,
			width:25
		},{
			name: 'posName',  
			header: "岗位名称",
			sortable: true,
			width: 25
		},{
			name: 'statusLabel',  
			header: "岗位状态",
			sortable: true,
			width:10
		}];
		
		this.structure = gridStructure;
		this.defaultSortField = 'posCode';
		this.keyField = 'posId';
		
		this.deleteMsg = '确认要删除选中的岗位吗？<br/><font color="red">注意</font>：删除岗位将删除该岗位下的所有子岗位。';
		this.deleteMsgBoxWidth = 350;
		
		Ext.ux.grid.PosGrid.superclass.initComponent.call(this);
		
		//如果启用了行双击事件的处理函数
		if(this.enabledDefaultRowDblclickEvent){
			this.on('rowdblclick', function(posGrid){
				var record = posGrid.getSelectionModel().getSelected();
				if(record != null){
					var posId = record.get('posId');
					this.showPosInfo(posId);
				}
			});
		}
		
		
		this.on('updateRecord', function(posId){
			this.showPosInfo(posId);
		});
		
		this.on('deleteRecord', function(grid, posIds){
			this.deletePos(posIds);
		});
	},
	//添加机构类型岗位
	addPosForOrg: function(orgId, orgName, parentPosId, parentPosName){
		win = new Ext.ux.org.omorg.PosDetailWindow({
				title: '新增下级岗位',
				orgId: orgId,
				orgName: orgName,
				parentPosId: parentPosId,
				parentPosName: parentPosName
			});
		win.show();
	},
	//添加工作组类型岗位
	addPosForGroup: function(groupId, groupName, parentPosId, parentPosName){
		var win = new Ext.ux.org.omgroup.GroupPosDetailWindow({
				title: '新增下级岗位'
		});
		win.initParentGroup(groupId, groupName);
		win.initParentPosition(parentPosId, parentPosName);
		win.show();
	},
	showPosInfo: function(posId){
		if(this.posType == 'GROUP'){
			var win = new Ext.ux.org.omgroup.GroupPosDetailWindow({
				title: '修改下级岗位',
				posId: posId
			});
			win.showPosInfo();
		}
		else{
			var win = new Ext.ux.org.omorg.PosDetailWindow({
				title: '修改下级岗位',
				posId: posId
			});
			win.showPosInfo();
		}
		
	},
	
	deletePos: function(posIds) {
		Ext.Ajax.request({
			url:'om-position!delete.action',
			success:function(){
				var orgTree = Ext.getCmp('orgTree');
				if( orgTree != null){
					orgTree.refreshOrg();
				}
				else{
					 Ext.getCmp('groupTree').refreshNode();
				}
				
				this.store.reload();
			},
			failure: function(form, action) {
				Ext.Msg.alert('系统提示', '删除岗位失败！');
			},
			method: 'POST',
			params:{
				posIds: posIds
			},
			waitMsg: '正在删除数据，稍候...',
			scope: this
		});
	}
});